<template>
  <div class="app-container">
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input
          placeholder="请输入用户名..."
          v-model="queryForm.query"
          clearable
        ></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="getUserListFun"
        >搜索</el-button
      ><el-button
        type="success"
        :icon="DocumentAdd"
        @click="handleDialogValue()"
        >新增
      </el-button>
      <el-popconfirm
        title="您确定批量删除这些记录吗？"
        @confirm="handleDelete(null)"
      >
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete"
            >批量删除</el-button
          >
        </template>
      </el-popconfirm>
    </el-row>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="avatar" label="头像" width="80" align="center">
        <template v-slot="scope">
          <img
            :src="baseurl + 'media/userAvatar/' + scope.row.avatar"
            width="50"
            height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="100"
        align="center"
      />
      <el-table-column prop="roles" label="拥有角色" width="200" align="center">
        <template v-slot="scope">
          <el-tag
            size="small"
            type="warning"
            v-for="item in scope.row.roleList"
          >
            {{ item.name }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="200" align="center" />
      <el-table-column
        prop="phonenumber"
        label="手机号"
        width="120"
        align="center"
      />
      <el-table-column prop="status" label="状态？" width="200" align="center">
        <template v-slot="{ row }">
          <el-switch
            v-model="row.status"
            @change="statusChangeHandle(row)"
            active-text="正常"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
        width="200"
        align="center"
      />
      <el-table-column
        prop="login_date"
        label="最后登录时间"
        width="200"
        align="center"
      />
      <el-table-column prop="remark" label="备注" />
      <el-table-column
        prop="action"
        label="操作"
        width="400"
        fixed="right"
        align="center"
      >
        <template v-slot="scope">
          <el-button
            type="primary"
            :icon="Tools"
            @click="handleRoleDialogValue(scope.row.id, scope.row.roleList)"
            >分配角色
          </el-button>
          <el-popconfirm
            v-if="scope.row.username != 'python222'"
            title="您确定要对这
个用户重置密码吗？"
            @confirm="handleResetPassword(scope.row.id)"
          >
            <template #reference>
              <el-button type="warning" :icon="RefreshRight"
                >重置密码</el-button
              >
            </template>
          </el-popconfirm>
          <el-button
            type="primary"
            v-if="scope.row.username != 'python222'"
            :icon="Edit"
            @click="handleDialogValue(scope.row.id)"
          ></el-button>
          <el-popconfirm
            v-if="scope.row.username != 'python222'"
            title="您确定要删除
这条记录吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <addUserFrom
      v-model="dialogVisible"
      :dialogVisible="dialogVisible"
      :id="id"
      :dialogTitle="dialogTitle"
      @initUserList="getUserListFun"
    ></addUserFrom>
    <!-- :userId="id" -->
    <!-- :userDialogTitle="userDialogTitle" -->
  </div>
</template>
<script setup>
import {
  Search,
  Delete,
  DocumentAdd,
  Edit,
  Tools,
  RefreshRight,
} from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { onMounted } from "vue";
import { getUserList, deleteUser } from "@/api/user";
import { ref } from "vue";
import { baseURL } from "@/util/config";
import addUserFrom from "@/views/sys/user/components/addUserForm.vue";
console.log("baseURL", baseURL);
const tableData = ref([]);
const total = ref(0);
const queryForm = ref({
  query: "",
  pageNum: 1,
  pageSize: 10,
});
const delBtnStatus = ref(true);
const multipleSelection = ref([]);
// 选中
const handleSelectionChange = (selection) => {
  console.log("勾选了");
  console.log(selection);
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length == 0;
};
// 删除
const handleDelete = async (id) => {
  var ids = [];
  if (id) {
    ids.push(id);
  } else {
    multipleSelection.value.forEach((row) => {
      ids.push(row.id);
    });
  }
  let result = await deleteUser({ ids });
  if (result.data.status == 200) {
    ElMessage({
      type: "success",
      message: "执行成功!",
    });
    getUserListFun();
  } else {
    ElMessage({
      type: "error",
      message: "执行失败",
    });
  }
};
// 新增或编辑表单
const dialogVisible = ref(false);
const id = ref("");
const dialogTitle = ref("");
const baseurl = baseURL;
const getUserListFun = async () => {
  console.log(123);

  try {
    let result = await getUserList(queryForm.value);
    console.log(result, "result");
    tableData.value = result.data.userList;
  } catch (err) {
    console.log(err);
  }
  // total.value = res.data.total;
};
onMounted(() => {
  getUserListFun();
});
const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  getUserListFun();
};
const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  getUserListFun();
};

// 新增或编辑
const handleDialogValue = (userId) => {
  dialogVisible.value = true;
  if (userId) {
    console.log("编辑");
    id.value = userId;
    dialogTitle.value = "编辑";
  } else {
    id.value = -1;
    dialogTitle.value = "新增";
    console.log("新增");
  }
};
// 自定义事件
const userDialog = (flag) => {
  console.log(flag, "flag");
  userDialogVisible.value = flag;
  // getUserListFun();
};
// 删除
// const handleDelete = async (userId) => {
//   let result = await deleteUser({ userId });
//   console.log(result, "result");
//   if (result.data.status == 200) {
//     ElMessage.success("删除成功");
//     getUserListFun();
//   } else {
//     ElMessage.error("删除失败");
//   }
// };
</script>
<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}
::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}
.el-tag--small {
  margin-left: 5px;
}
</style>
